package com.wd.elec.service.impl;

import com.wd.elec.domain.*;
import com.wd.elec.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.wd.elec.controller.utils.DateTimeUtils.*;

/**
 * @className: AttendCulculateService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2023/11/16
 **/
@Service
public class AttendCalculateService {

    @Resource
    IAttendGroupService attendGroupService;

    @Resource
    IAttendScheduleService attendScheduleService;

    @Resource
    IAttendResultService attendResultService;

    @Resource
    IAttendRecordService attendRecordService;

    @Resource
    IStaffInfoService staffInfoService;

    public void calculateAttend(Date attendDate) {
        List<AttendGroup> attendGroups = attendGroupService.selectAttendGroupList(new AttendGroup());
        //按照考勤分组进行考勤计算

        for (int i = 0; i < attendGroups.size(); i++) {
            AttendGroup attendGroup = attendGroups.get(i);
            calculateAttend(attendGroup, attendDate);
        }
    }

    public void calculateAttend(AttendGroup attendGroup, Date attendDate) {
        AttendSchedule attendSchedule = attendScheduleService.selectAttendScheduleByScheduleId(attendGroup.getScheduleId());
        StaffInfo sInfo = new StaffInfo();
        sInfo.setGroupId(attendGroup.getGroupId());
        List<StaffInfo> staffInfos = staffInfoService.selectAllocatedGroupList(sInfo);

        Date sStartTime = parseToDate(attendSchedule.getsStartTime(), HH_MM);
        Date sEndTime = parseToDate(attendSchedule.getsEndTime(), HH_MM);
        Date eStartTime = parseToDate(attendSchedule.geteStartTime(), HH_MM);
        Date eEndTime = parseToDate(attendSchedule.geteEndTime(), HH_MM);

        if (attendSchedule != null) {
            //根据考勤分组和考勤日期，查询通行记录
            List<AttendRecord> attendRecords = attendRecordService.selectAttendRecordByGroupId(attendGroup.getGroupId(), attendDate);
            Map<String, AttendResult> attendMap = new HashMap<>();

            for (StaffInfo staffInfo : staffInfos) {

                AttendResult attendResult = attendMap.get(staffInfo.getStaffNum());
                if (attendResult == null) {
                    attendResult = new AttendResult();
                    attendResult.setStaffNum(staffInfo.getStaffNum());
                    attendResult.setAttendDate(attendDate);
                    attendResult.setGroupId(attendGroup.getGroupId());
                }
                //遍历通行记录，生成考勤记录
                for (int i = 0; i < attendRecords.size(); i++) {
                    AttendRecord attendRecord = attendRecords.get(i);

                    if (attendRecord.getStaffNum().equals(staffInfo.getStaffNum())) {

                        Date aTime = attendRecord.getAttendTime();
                        //上班时间
                        if (isBeforeByTime(sStartTime, aTime)
                                && isBeforeByTime(aTime, sEndTime)) {
                            if (attendResult.getCheckinTime() == null) {
                                attendResult.setCheckinTime(aTime);
                            }
                            if (aTime.before(attendResult.getCheckinTime())) {
                                attendResult.setCheckinTime(aTime);
                            }
                        }

                        //下班时间
                        if (isBeforeByTime(eStartTime, aTime) && isBeforeByTime(aTime, eEndTime)) {
                            if (attendResult.getCheckoutTime() == null) {
                                attendResult.setCheckoutTime(aTime);
                            }
                            if (attendResult.getCheckoutTime().before(aTime)) {
                                attendResult.setCheckoutTime(aTime);
                            }
                        }
                    }
                }
                attendMap.put(staffInfo.getStaffNum(), attendResult);
            }

            calculateResult(attendMap, attendSchedule, attendDate, attendGroup.getGroupId());
        }
    }


    public void calculateResult(Map<String, AttendResult> attendMap, AttendSchedule attendSchedule, Date attendDate, Long groupId) {
        Date startTime = parseToDate(attendSchedule.getStartTime(), HH_MM);
        Date endTime = parseToDate(attendSchedule.getEndTime(), HH_MM);

        List<AttendResult> list = new ArrayList<>();

        for (String staffNum : attendMap.keySet()
        ) {
            AttendResult attendResult = attendMap.get(staffNum);
            attendResult.setAttendResult("正常");
            if (isBeforeByTime(startTime, attendResult.getCheckinTime())) {
                attendResult.setAttendResult("退到");
            }

            if (isBeforeByTime(attendResult.getCheckoutTime(), endTime)) {
                attendResult.setAttendResult("早退");
            }

            if (attendResult.getCheckinTime() == null) {
                attendResult.setAttendResult("漏打卡");
            }

            if (attendResult.getCheckoutTime() == null) {
                attendResult.setAttendResult("漏打卡");
            }

            if (attendResult.getCheckinTime() == null && attendResult.getCheckoutTime() == null) {
                attendResult.setAttendResult("旷工");
            }
            list.add(attendResult);
        }

        attendMap.clear();

        attendResultService.refreshDateAttendResult(list, attendDate, groupId);
    }

}
